package com.newcapec.erp.ui;

import com.newcapec.erp.bean.StoreEntity;
import com.newcapec.erp.exception.MyAppException;
import com.newcapec.erp.main.DataInit;
import com.newcapec.erp.utils.XMLFReader;
import com.newcapec.erp.utils.XMLFWriter;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class UpdateStoreUI extends JFrame {

	private JPanel contentPane;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	private JLabel label_5;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private StoreEntity storeEntity;
	private int index;
	private int currentPage;

public UpdateStoreUI(){}
	/**
	 * Create the frame.
	 */
	public UpdateStoreUI(StoreEntity storeEntity,int index,int currentPage) {
		this.storeEntity = storeEntity;
		this.index = index;
		this.currentPage = currentPage;
		setIconImage(Toolkit.getDefaultToolkit().getImage("images/logo.png"));
		setTitle("修改储位信息");
		setBounds(100, 100, 452, 568);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		label = new JLabel(DataInit.storeColumnNames.get(0));
		label.setBounds(20,34,100,37);
		textField = new JTextField();
		textField.setBounds(80, 34, 341, 37);
		textField.setText(storeEntity.getBasedWarehouse());
		textField.setColumns(10);
		contentPane.add(label);
		contentPane.add(textField);

		label_1 = new JLabel(DataInit.storeColumnNames.get(1));
		label_1.setBounds(20,107,100,37);
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(80, 107, 341, 37);
		textField_1.setText(storeEntity.getBasedCustomer());
		contentPane.add(label_1);
		contentPane.add(textField_1);

		label_2 = new JLabel(DataInit.storeColumnNames.get(2));
		label_2.setBounds(20,185,100,37);
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(80, 185, 341, 37);
		textField_2.setText(storeEntity.getStoreType());
		contentPane.add(label_2);
		contentPane.add(textField_2);

		label_3 = new JLabel(DataInit.storeColumnNames.get(3));
		label_3.setBounds(20,254,100,37);
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(80, 254, 341, 37);
		textField_3.setText(storeEntity.getStoreAttr());
		contentPane.add(label_3);
		contentPane.add(textField_3);

		label_4 = new JLabel(DataInit.storeColumnNames.get(4));
		label_4.setBounds(20,328,100,37);
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(80, 328, 341, 37);
		textField_4.setText(storeEntity.getProductAttr());
		contentPane.add(label_4);
		contentPane.add(textField_4);

		label_5 = new JLabel(DataInit.storeColumnNames.get(5));
		label_5.setBounds(20,402,100,37);
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setBounds(80, 402, 341, 37);
		textField_5.setText(storeEntity.getStatus());
		contentPane.add(label_5);
		contentPane.add(textField_5);

		List<String> allWarehouses = XMLFReader.getAllWarehouseNames();
		JButton button = new JButton("保存");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = textField.getText();
				//校验所属仓库字段
				boolean b = allWarehouses.stream().noneMatch(text::equals);
				if(b){
					try {
						throw new MyAppException("1");
					} catch (MyAppException updateException) {
						JOptionPane.showMessageDialog(null, "仓库"+text+"不存在，修改失败！", "提示",JOptionPane.ERROR_MESSAGE);
						return;
					}
				}
				String text1 = textField_1.getText();
				String text2 = textField_2.getText();
				String text3 = textField_3.getText();
				String text4 = textField_4.getText();
				String text5 = textField_5.getText();
				StoreEntity newStoreEntity = new StoreEntity(text, text1, text2, text3, text4, text5);
				XMLFWriter.UpdateStore(index,newStoreEntity);
				JOptionPane.showMessageDialog(null, "恭喜你，修改成功.", "提示",JOptionPane.PLAIN_MESSAGE);
				Vector<Vector<String>> data = XMLFReader.getData(DataInit.TYPE_STORE);
				MainUI.updateMainUI(DataInit.TYPE_STORE,data,currentPage);
				dispose();
			}
		});
		button.setBounds(221, 481, 93, 23);
		contentPane.add(button);
		setLocationRelativeTo(null);
		setVisible(true);

	}

}

